package com.sinochem.yunlian.ship.base.biz;

import com.sinochem.yunlian.ship.base.vo.request.ReqMaterialCategoryFavVo;
import com.sinochem.yunlian.ship.common.biz.BaseBiz;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinochem.yunlian.ship.base.entity.BsMaterialCategoryFav;
import com.sinochem.yunlian.ship.base.mapper.BsMaterialCategoryFavMapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author wh
 * @email ${email}
 * @date 2017-12-11 15:46:51
 */
@Service
public class BsMaterialCategoryFavBiz extends BaseBiz<BsMaterialCategoryFavMapper,BsMaterialCategoryFav> {

    @Autowired
    private BsMaterialCategoryBiz bsMaterialCategoryBiz;

    public List<BsMaterialCategoryFav> selectMaterialCategroyFavListByUserId(Long companyId){

        return mapper.selectMaterialCategroyFavListByUserId(companyId);
    }

    public String AddMaterialCategroyFav(long userId, long companyId, ReqMaterialCategoryFavVo vo){
        //检查是否已经存在
        int had = mapper.checkMaterialCategroyFav(companyId, vo.getMaterialCategoryId());
        if (had > 0){
            return "货品已经收藏";
        }

        //检查货品ID是否合法
        String materialCategoryName = bsMaterialCategoryBiz.getBsMaterialCategoryName(vo.getMaterialCategoryId());
        if (StringUtils.isBlank(materialCategoryName)){
            return "参数错误，货品不存在";
        }

        //插入新数据
        BsMaterialCategoryFav fav = new BsMaterialCategoryFav();
        fav.setCompanyId(companyId);
        fav.setMaterialCategoryId(vo.getMaterialCategoryId());
        fav.setMaterialCategoryName(materialCategoryName);
        fav.setDensity(vo.getDensity());
        fav.setTemperature(vo.getTemperature());
        fav.setCreateBy(userId);
        fav.setCreateTime(new Date());
        fav.setUpdateBy(userId);
        mapper.insert(fav);
        return null;
    }

    public String DeleteMaterialCategroyFav(long userId, long companyId, long materialCategoryId){
        //检查是否已经存在
        int had = mapper.checkMaterialCategroyFav(companyId, materialCategoryId);
        if (had == 0){
            return "货品未收藏";
        }

        //删除数据
        mapper.deleteMaterialCategroyFav(companyId, materialCategoryId);
        return null;
    }

    public String updateMaterialCategroyFav(long companyId,ReqMaterialCategoryFavVo vo){

        //检查是否已经存在
        int had = mapper.checkMaterialCategroyFav(companyId, vo.getMaterialCategoryId());
        if (had == 0){
            return "货品未收藏";
        }

        //更新数据
        Map inMap = new HashMap();
        inMap.put("companyId",companyId);
        inMap.put("materialCategoryId", vo.getMaterialCategoryId());
        inMap.put("density", vo.getDensity());
        inMap.put("temperature", vo.getTemperature());

        mapper.updateMaterialCategoryFav(inMap);
        return null;
    }
}